﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace Guide
{
    public partial class Select2 : System.Web.UI.Page
    {


        protected void Page_Load(object sender, EventArgs e)
        {
            int i = 0;

            foreach (var item in (List<string>)Session["listSelectClusterName"])  //放到if (!IsPostBack) 里面
            {
                var control = (Stock)LoadControl("Stock.ascx");
                control.ID = "Hello" + i;
                control.LabelContext = item;
                control.ListProvince = ((List<Province>)Session["listprovince"]).Where(o => o.ClusterID == item).ToList();
                Hello.Controls.Add(control);
                i = i + 1;
            }

            if (!IsPostBack)
            {


                //var control1 = (Stock)LoadControl("Stock.ascx");
                //Hello.Controls.Add(control1);
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            var xx = this;
            List<Province> list = new List<Province>();
            int i = 0;
            while (true)
            {
                var control = (Stock)FindControl("Hello" + i);
                if (control == null)
                {
                    break;
                }

                StringBuilder sb = new StringBuilder();
                //GridView1.AllowPaging = false;
                //GridView1.DataBind();
                foreach (GridViewRow row in control.GridView.Rows)
                {
                    bool isChecked = false;
                    if ((row.FindControl("CheckBox1") as CheckBox).Checked == true)
                    {
                        isChecked = true;
                    }
                    else
                    {
                        isChecked = false;
                    }

                    if (isChecked)
                    {
                        var provinceID = row.FindControl("ProvinceID") as HiddenField;
                        var provinceName = row.FindControl("ProvinceCName") as Label;
                        var clusterId = row.FindControl("ClusterID") as Label;
                        list.Add(new Province()
                        {
                            ClusterID = clusterId.Text,
                            ProvinceCName = provinceName.Text,
                            ProvinceID = provinceID.Value
                        });
                    }
                }
                i++;
                
            }
            Session["listProvincePrint"] = list;
            Response.Redirect("/Printer.aspx");
        }

    }
}